using AutoMapper;
using B.S.XZYData.Api.Read.Application.Command.Role;
using B.S.XZYData.Domain.RBAC;
using B.S.XZYData.ErrorCode;
using B.S.XZYData.Infrastructure.Interface;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Read.Application.Handler.Role
{
    public class GetRoleAllHandler : IRequestHandler<GetRoleAllCommand, ApiResult<List<RoleModel>>>
    {
        private readonly IBaseRepository<RoleModel> _roleRepo;
        private readonly IBaseRepository<PermissionRoleModel> _rolePermissionRepo;
        private readonly IBaseRepository<PermissionModel> _permissionRepo;
        private readonly IMapper _mapper;

        public GetRoleAllHandler(
            IBaseRepository<RoleModel> roleRepo,
            IBaseRepository<PermissionRoleModel> rolePermissionRepo,
            IBaseRepository<PermissionModel> permissionRepo,
            IMapper mapper)
        {
            _roleRepo = roleRepo;
            _rolePermissionRepo = rolePermissionRepo;
            _permissionRepo = permissionRepo;
            _mapper = mapper;
        }

        public async Task<ApiResult<List<RoleModel>>> Handle(GetRoleAllCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<List<RoleModel>>();
            try
            {
                res.Data = _roleRepo.GetAll().Where(x => !x.IsDeleted).ToList();
                res.Msg = "查询角色列表成功!";
                res.Code = ApiEnum.Success;
            }
            catch (Exception ex)
            {
                res.Msg = "查询角色列表异常：" + ex.Message;
                res.Code = ApiEnum.Error;
            }
            return await Task.FromResult(res);
        }
    }
} 